package org.dftproject.genesis.ui.pages.summary.sources;

import org.dftproject.genesis.data.IAttribute;
import org.dftproject.genesis.data.genealogy.ISource;
import org.dftproject.genesis.ui.figures.ButtonPainter;
import org.dftproject.genesis.ui.figures.TooltipLabel;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Cursors;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.MouseMotionListener;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;

public class FootnoteMarker extends Figure {
	
	private final static Color textColor = ColorConstants.black;
	private final static Color normalColor = new Color(null, 0xAA, 0xAA, 0xAA);
	private final static Color hoverColor = ColorConstants.black;
	
	private final FootnoteContainer footnoteContainer;
	private Footnote footnote;
	
	private final Label label;
	private boolean hover;

	public FootnoteMarker(FootnoteContainer footnoteContainer) {
		this.footnoteContainer = footnoteContainer;
		
		setLayoutManager(new StackLayout());
		
		setForegroundColor(textColor);
		
		label = new Label();
		add(label);

		setCursor(Cursors.HAND);
		addListeners();
		
		setToolTip(new TooltipLabel("See footnote"));
		
		setBorder(new MarginBorder(1, 5, 2, 5));
	}
	
	public void addSource(IAttribute sourceAttribute) {
		footnoteContainer.addSource(sourceAttribute, this);
	}
	
	@Override
	public void paintFigure(Graphics graphics) {
		if (isHover()) {
			ButtonPainter painter = new ButtonPainter();
			painter.drawButton(graphics, ButtonPainter.HOVER, bounds);
		} else {
			Rectangle bounds = getBounds();
			
			graphics.pushState();
			graphics.setAntialias(SWT.ON);
			graphics.setForegroundColor(hover ? hoverColor : normalColor);
			graphics.drawRoundRectangle(bounds.getResized(-1, -1), 6, 6);
			graphics.popState();
		}
	}

	protected void addListeners() {
		MouseTracker tracker = new MouseTracker();
		addMouseMotionListener(tracker);
		addMouseListener(tracker);
	}

	public boolean isHover() {
		return hover;
	}

	public void setHover(boolean hover) {
		this.hover = hover;
	}

	protected void handleSelected() {
		if (footnote != null)
			footnoteContainer.scrollTo(footnote);
	}

	protected class MouseTracker extends MouseMotionListener.Stub implements MouseListener {

		private boolean mouseDown;

		public void mousePressed(MouseEvent me) {
			if (me.button == 1) {
				requestFocus();
				me.consume();
				mouseDown = true;
			}
		}

		public void mouseReleased(MouseEvent me) {
			if (mouseDown && me.button == 1) {	
				if (getBounds().contains(me.getLocation()))
					handleSelected();
				mouseDown = false;
			}
		}

		public void mouseDoubleClicked(MouseEvent me) {
		}

		@Override
		public void mouseEntered(MouseEvent me) {
			setHover(true);
			repaint();
		}

		@Override
		public void mouseExited(MouseEvent me) {
			setHover(false);
			repaint();
		}

		@Override
		public void mouseDragged(MouseEvent me) {
			setHover(getBounds().contains(me.getLocation()));
			repaint();
		}

	}

	public void setFootnote(ISource source, Footnote footnote) {
		label.setText(Integer.toString(footnote.getNumber()));
		this.footnote = footnote;
	}

}